package com.woyou.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.EBean.Scope;

import com.woyou.bean.CodeListResult;
import com.woyou.bean.Gift;
import com.woyou.bean.OrderGiftItem;
import com.woyou.bean.rpc.ActivitiesReq;
import com.woyou.bean.rpc.MD5Req;
import com.woyou.model.rpc.ShopService;

import android.util.Log;
import retrofit.RetrofitError;

/**
 * 活动业务逻辑封装
 * 
 * @author longtao.li
 * 
 * @param <T>
 * @param <E>
 */
@SuppressWarnings({"unused"})
@EBean(scope = Scope.Singleton)
public class ActivitiesModel extends SuperModel {

	private static final String TAG = "ActivitiesController";
	private ShopService mShopService;
	CodeListResult<Gift> result;

	@AfterInject
	// 在构造函数调用之后执行
	@Override
	public void onInit() {
		mShopService = mNeWrapper.getNetService(ShopService.class);
	}
	
	/**
	 * 记录赠品的已选数量
	 */
	private Map<String, Integer> counts = new HashMap<String, Integer>();
	
	/**
	 * 记录数量
	 * @author:longtao.li
	 * @param code
	 * @param num
	 */
	public void putGiftNum(String code, Integer num){
		counts.put(code, num);
	}
	
	/**
	 * 取出数量
	 * @author:longtao.li
	 * @param code
	 * @return
	 */
	public Integer getGiftNum(String code){
		Integer num = counts.get(code);
		return num;
	}
	
	/**
	 * 删除选择的赠品
	 * @author:longtao.li
	 * @param code
	 */
	public void removeGiftNum(String code){
		counts.remove(code);
	}
	
	/**
	 * 过滤数量(上次请求有的赠品在这次没有传下来)
	 * @author:longtao.li
	 */
	public void giftNumFilter(){
		
		if( result == null ){
			return;
		}
		List<Gift> list = result.getList();
		
		HashMap<String, Integer> temp = new HashMap<String, Integer>();
		for( Gift gift : list ){
			if( gift == null ){
				continue;
			}
			Integer giftNum = counts.get(gift.getaId());
			if( giftNum != null ){
				temp.put(gift.getaId(), giftNum);
			}
		}
		counts.clear();
		counts.putAll(temp);
	}
	
	public CodeListResult<Gift> giftResult(){
		return result;
	}
	
	/**
	 * 获取当前赠品列表
	 * @return
	 */
	public List<Gift> getGiftList(){
		List<Gift> giftList = new ArrayList<Gift>();
		if( result!=null && 1 == result.code && result.list!=null ){
			giftList.addAll(result.list);
		}
		return giftList;
	}
	
	/**
	 * 转换成下单时的实体
	 * @return
	 */
	public List<OrderGiftItem> getOrderGiftList(){
		List<OrderGiftItem> giftList = new ArrayList<OrderGiftItem>();
		if( result == null || result.getList() == null ){
			return giftList;
		}
		for(Gift g : result.getList()){
			if( g.getQty() <= 0 ){
				continue;
			}
			OrderGiftItem gift = new OrderGiftItem();
			gift.setaId(g.getaId());
			gift.setQty(g.getQty());
			giftList.add(gift);
		}
		return giftList;
	}
	
	/**
	 * 状态 1条件满足,2条件不足,-1无活动
	 * @author:longtao.li
	 * @return
	 */
	public int status(){
		return result.code;
	}
	
	

	/**
	 * 获取商品活动
	 * @param 
	 * @return
	 */
	public CodeListResult<Gift> queryActivities(ActivitiesReq req)
			throws RetrofitError {
		CodeListResult<Gift> tempResult = null;
		try {
			MD5Req<ActivitiesReq> md5Req = new MD5Req<ActivitiesReq>(req);
			tempResult = mShopService.queryActivity(
					md5Req.jsonParams, md5Req.isEncrypted, md5Req.timeStamp,
					md5Req.randomNum, md5Req.sign);
		} catch (RetrofitError e) {
		}
		
//		CodeListResult<Gift>  tempResult = mockQueryActivities();
		result = tempResult;
		if(tempResult!=null&&tempResult.getCode()==1){
			List<Gift> list = new ArrayList<Gift>();
			for (Gift g : tempResult.getList()) {
				Integer qty = counts.get(g.getaId());
				if( qty == null ){
					qty = 0;
					if( g.getPrice() <= 0 ){
						qty = g.getMaxQty();
						if( qty == 0 ){
							qty = 1;
						}
					}
				}else{
					if( qty > g.getMaxQty() ){
						qty = g.getMaxQty();
					}
				}
				g.setQty(qty);
				counts.put(g.getaId(), g.getQty());

				boolean contains = priceMarkupList.contains(g);
				if( contains ){
					list.add(g);
				}
			}
			priceMarkupList.clear();
			priceMarkupList.addAll(list);//记录已经提示过的赠品
		}else{
			priceMarkupList.clear();
		}
		if( result!=null&&result.getList() != null )
		   Log.i(TAG, "获得"+result.getList().size()+"个赠品");
		return result;
	}

	private CodeListResult<Gift> mockQueryActivities() {
		CodeListResult<Gift> res = new CodeListResult<Gift>();
		List<Gift> list = new ArrayList<Gift>();
		res.code = 1;
		res.list = list;
		Gift g = new Gift();
		g.setaId("a01");
		g.setaName("满10元送可乐");
		g.setMaxQty(3);
		g.setPrice(3);
		g.setUnit("罐");
		res.list = list;
		Gift g1 = new Gift();
		g1.setaId("a02");
		g1.setaName("满10元送年糕");
		g1.setMaxQty(3);
		g1.setPrice(0);
		g1.setUnit("份");
		
		list.add(g);
		list.add(g1);
		return res;
	}
	
	/**
	 * 清除赠品列表
	 */
	public void clearActiviesCenter(){
		result = null;
		counts.clear();
		priceMarkupList.clear();
	}

	/**
	 * 是否满足查询条件
	 * @return
	 */
	public boolean isMeet() {
		return result == null || 2 == result.code || 1 == result.code;
		
	}
	
	/**
	 * 已经提示过的加价购赠品
	 */
	List<Gift> priceMarkupList = new ArrayList<Gift>();
	/**
	 * 是否含有加价购商品
	 * @return
	 */
	public boolean isMeet1() {
		if( result == null || result.list == null ){
			return false;
		}
		List<Gift> list = new ArrayList<Gift>();
		for( Gift g : result.getList() ){
			if( g.getPrice()>0 ){
				list.add(g);
				if( !priceMarkupList.contains(g) ){
					priceMarkupList.add(g);
				}
			}
		}
		if( list.size()>0 ){
			return true;
		}
		return false;
	}
	
	/**
	 * 新拉取的赠品是否已经提示过
	 * @return
	 */
	public boolean isAlreadySuggested(List<Gift> list){
		for( Gift g : list ){
			if( g.getPrice()>0 ){
				list.add(g);
			}
		}
		return priceMarkupList.containsAll(list);
	}
	/**
	 * 获取查询结构
	 * @return
	 */
	public CodeListResult<Gift> getResult(){
		return result;
	}
}
